﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Husb.DataUtil;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using System.Data;

using GroundWater.BusinessActions;
using System.Text;

namespace GroundWater
{
    public partial class ExecuteSqlForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnBulk_Click(object sender, EventArgs e)
        {
            StringReader reader = new StringReader(txtSql.Text);
            string line;
            int cnt = 0;
            while ((line = reader.ReadLine()) != null)
            {
                //if (line.Length > 5)
                //{
                //    string[] ds = line.Split('\t');
                //    if (ds.Length > 5 )
                //    {
                //        Husb.DataUtil.DataAccessUtil.ExecuteNonQuery(reader
                //    }
                //}
                if (line.Trim().Length == 0) continue;
                int i = Husb.DataUtil.DataAccessUtil.ExecuteNonQuery(line, new List<DatabaseParameter>(), false, false);
                cnt += i;
            }

            txtSql.Text = cnt.ToString();
            
        }

        protected void btnSingle_Click(object sender, EventArgs e)
        {
            int i = Husb.DataUtil.DataAccessUtil.ExecuteNonQuery(txtSql.Text.Trim(), new List<DatabaseParameter>(), false, false);

            txtSql.Text = i.ToString();
        }

        protected void btnClearCache_Click(object sender, EventArgs e)
        {
            //Husb.DataUtil.DataAccessUtil.UpdateCacheDependency(new string[] { "", "Users", "Wells", "WaterSource" });

            CacheFactory.GetCacheManager().Remove("Authorization");
            CacheFactory.GetCacheManager().Remove("Users");
            CacheFactory.GetCacheManager().Remove("Wells");
            CacheFactory.GetCacheManager().Remove("WaterSource");
        }

        protected void btnFetch_Click(object sender, EventArgs e)
        {
            if (txtSql.Text.Trim().Length == 0)
                return;
            DataTable dt = Husb.DataUtil.DataAccessUtil.GetDataTable(txtSql.Text.Trim(), new List<DatabaseParameter>(), false, false, null);

            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        protected void btnAuth_Click(object sender, EventArgs e)
        {
            string menu = txtSql.Text.Trim();

            DataTable dt = Users.FetchMenusAll();
            var menus = dt.Select("Name = '" + menu + "'");
            if (menus == null || menus.Length == 0)
                return;
            var m = menus[0]["Id"].ToString();

            DataTable dt0 = Users.FetchAuthByUserId(HttpContext.Current.User.Identity.Name.ToLower(), m);

            string response = "菜单名称： " + menu + ", 菜单编号：" + m + "," ;
            if (dt0.Rows.Count > 0)
            {
                response = response + "用户名：" + dt0.Rows[0][0].ToString() + ", 菜单编号：" + dt0.Rows[0][1].ToString();
            }
            else
            {
                response = response + HttpContext.Current.User.Identity.Name.ToLower() + "没有匹配到菜单" + m;
            }
            txtSql.Text = response;
            DataTable dt1 = Users.FetchAuth();
            GridView1.DataSource = dt1;
            GridView1.DataBind();

            // SiteUtil.GetString(SiteConstants.CurrentUser)
        }


        protected void btnSQL_Click(object sender, EventArgs e)
        {
            string sql = @"UPDATE WR_GWSWLL_B SET WSWLCD = 'D370282DG0{0}Q' WHERE WSWLCD = '0000000000{0}'
UPDATE WR_GWLSPL_S SET WSWLCD = 'D370282DG0{0}Q', CONTROLLERID = 'D370282DG0{0}Q' WHERE WSWLCD = '0000000000{0}'

";
            StringBuilder sb = new StringBuilder("");
            for (int i = 1; i < 91; i++)
            {
                string item = Husb.Util.CommonUtil.FillZero(i, 2);

                sb.Append(String.Format(sql, item));

            }
            txtSql.Text = sb.ToString();

        }

    }
}
